package com.jinxin.platform.cdsmsmgr.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdsmsmgr.alisms.SmsFactory;
import com.jinxin.platform.cdsmsmgr.mapper.SmsAkInfoMapper;
import com.jinxin.platform.cdsmsmgr.pojo.domains.SmsAkInfo;
import com.jinxin.platform.cdsmsmgr.pojo.domains.SysUserVO;
import com.jinxin.platform.cdsmsmgr.pojo.entity.*;
import com.jinxin.platform.cdsmsmgr.rpc.SysUserRpc;
import com.jinxin.platform.cdsmsmgr.service.ISmsAkInfoService;
import com.jinxin.platform.cdsmsmgr.utils.TxSmsUtil;
import com.jinxin.platform.cdsmsmgr.utils.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-06-28
 * @Author: Lingnan
 **/
@Service
@Slf4j
public class SmsAkInfoServiceImpl implements ISmsAkInfoService {

    @Autowired
    private SmsAkInfoMapper smsAkInfoMapper;
    @Autowired
    private SysUserRpc sysUserRpc;

    @Override
    public ResultEntity saveOrUpdate(SmsAkInfo smsAkInfo) {
        SysUserVO currentUser = sysUserRpc.getCurrentUser();
        if(currentUser == null) {
            return new ResultEntity(CommonCodeEntity.NO_LOGIN,"未登录用户","");
        }
        String thirdAccountType = smsAkInfo.getThirdAccountType();
        String akId = smsAkInfo.getAkId();
        String akSecret = smsAkInfo.getAkSecret();
        if(Constant.TX_ACCOUNT.equals(thirdAccountType)){
            TxSmsTemplateQueryParam queryParam = new TxSmsTemplateQueryParam();
            queryParam.setSecretId(akId);
            queryParam.setSecretKey(akSecret);
            queryParam.setInternational(Constant.SMS_TYPE_DOMESTIC);
            JsonResult jsonResult = TxSmsUtil.describeSmsTemplateList(queryParam);
            if(!jsonResult.getSuccess()){
                return new ResultEntity(CommonCodeEntity.PARAM_ERR,jsonResult.getMsg(),"");
            }
        }else {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put(SmsConstant.ACCESS_KEY_ID,akId);
            paramMap.put(SmsConstant.ACCESS_SECERT,akSecret);
            paramMap.put(SmsConstant.QUERY_PAGE_SIZE,SmsConstant.PAGE_INDEX);
            try {
                SmsFactory.aliQuerySmsTemplateList(paramMap);
            } catch (ClientException e) {
                e.printStackTrace();
                log.error(e.toString());
                return new ResultEntity(CommonCodeEntity.PARAM_ERR,"API密钥对错误","");
            }
        }
        int count;
        if(!StringUtils.isEmpty(smsAkInfo.getId())){
            count = smsAkInfoMapper.updateByKey(smsAkInfo);
        }else {
            smsAkInfo.setCreateTime(LocalDateTime.now());
            smsAkInfo.setId(Utils.getMillPrimaryKey());
//            smsAkInfo.setUserAccount(currentUser.getUserAccount());
            smsAkInfo.setUserAccount(Constant.ACCOUNT_ADMIN);
            count = smsAkInfoMapper.insert(smsAkInfo);
        }
        if(count > 0){
            return new ResultEntity(CommonCodeEntity.SUC_CODE,"suc",smsAkInfo);
        }else {
            return new ResultEntity(CommonCodeEntity.FAIL,"保存失败","");
        }
    }

    @Override
    public ResultEntity getAkInfoByUser() {
        SmsAkInfo akInfo = getAkInfo();
        if(akInfo != null){
            return new ResultEntity(CommonCodeEntity.SUC_CODE,"suc",akInfo);
        }
        return new ResultEntity(CommonCodeEntity.FAIL,"获取ak信息失败","");
    }

    private SmsAkInfo getAkInfo() {
        HashMap param = new HashMap();
        param.put("userAccount",Constant.ACCOUNT_ADMIN);
        return smsAkInfoMapper.selectOne(param);
//        SysUserVO currentUser = sysUserRpc.getCurrentUser();
//        if(currentUser == null) {
//            return null;
//        }
//        String userAccount = currentUser.getUserAccount();
//        if(!StringUtils.isEmpty(userAccount)){
//            HashMap param = new HashMap();
//            //            param.put("userAccount",userAccount);
//            param.put("userAccount","admin");
//            return smsAkInfoMapper.selectOne(param);
//        }
//        return null;
    }
}
